<template>
  <div class="input">
    <div style="margin-top: 15px;">
      <p class="state">
        状态：<el-select v-model="value" placeholder="请选择" class="sele">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        名称：<el-input placeholder="请输入内容" v-model="name" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="Search"></el-button>
        </el-input>
      </p>
    </div>
    <div class="btn">
      <el-button type="primary" @click="dialogFormVisible = true"
        >新增产品分类</el-button
      >
      <el-dialog title="创建产品分类" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              autocomplete="off"
              placeholder="数码产品"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form :model="form">
          <el-form-item label="父级" :label-width="formLabelWidth">
            <el-input
              v-model="form.region"
              autocomplete="off"
              placeholder="每日新品"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-upload
          action="https://jsonplaceholder.typicode.com/posts/"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          class="updata"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
        <el-input-number
          v-model="num"
          :min="1"
          :max="10"
          label="描述文字"
        ></el-input-number>
        <p>
          状态：<el-tooltip :content="'显示' + value" placement="top">
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="100"
              inactive-value="0"
            >
            </el-switch>
          </el-tooltip>
        </p>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" v-model="val">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          fixed
          prop="name"
          label="名称"
          width="200"
        ></el-table-column>
        <el-table-column prop="type" label="类型" width="160"></el-table-column>
        <el-table-column props="img" label="商品图片" width="160" >
          <template slot-scope="scope">
            <img :src="scope.row.img" alt style="height:50px;" />
          </template>
        </el-table-column>
        <el-table-column prop="num" label="排序" width="170"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="上架"
              inactive-text="下架"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="dialogFormVisible = true"
              size="small"
              >添加子目录</el-button
            >
            <el-button type="text">编辑</el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        style="margin-top:20px"
        @current-change="pageChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "选项1",
          label: "全部"
        },
        {
          value: "选项2",
          label: "商品名称"
        },
        {
          value: "选项3",
          label: "商品ID"
        },
        {
          value: "选项4",
          label: "商品类型"
        }
      ],
      value: "",
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: ""
      },
      formLabelWidth: "120px",
      dialogImageUrl: "",
      dialogVisible: false,
      num: 1,
      tableData: [],
      pageIndex: 1,
      pageSize: 5,
      total: 0,
      val:'',
      name:""
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    pageChange(ind) {
      this.pageIndex = ind;
      this.$api
        .shopSort({ pageIndex: this.pageIndex, pageSize: this.pageSize })
        .then(res => {
          if (res.code === 1) {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } 
        });
    },
    Search(){
      this.tableData.filter((item)=>item.name.includes(this.val))
    }
  },
  created() {
    this.pageChange(1);
  }
};
</script>

<style lang="scss" scoped>
.btn {
  margin: 20px 0;
}
.input-with-select {
  width: 400px;
  text-align: left;
  /* margin-top: 20px; */
}
.sele {
  width: 140px;
}
.el-input-number,
p,
.updata {
  margin-left: 50px;
 /*  margin-top: 30px; */
}
.state,
.btn {
  margin-left: 20px;
}
</style>